//
//  UserInfo.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 10/4/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "UserInfo.h"
#import "Utility.h"

@implementation UserInfo
@synthesize _userGold = mUserGold;
@synthesize _numItemGlove = mNumItemGlove;
@synthesize _numItemMagnet = mNumItemMagnet;
@synthesize _numItemTime = mNumItemTime;
@synthesize _numItemXray = mNumItemXRay;

+(UserInfo *) getInstance
{
    static UserInfo *mInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[[Utility getInstance] loadObjectForKey:KEY_USER_INFO] retain];
        
        if (mInstance == nil)
            mInstance = [[UserInfo alloc] init];
    });
    
    return mInstance;
}

-(void) encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt64:mUserGold forKey:KEY_PAYMENT_GOLD];
    [encoder encodeInt:mNumItemGlove forKey:KEY_ITEM_NUM_GLOVE];
    [encoder encodeInt:mNumItemXRay forKey:KEY_ITEM_NUM_XRAY];
    [encoder encodeInt:mNumItemTime forKey:KEY_ITEM_NUM_TIME];
    [encoder encodeInt:mNumItemMagnet forKey:KEY_ITEM_NUM_MAGNET];
}

-(id) initWithCoder:(NSCoder *)decoder
{
    if (self = [super init])
    {
        CCLOG(@"init codeer");
        mUserGold = [decoder decodeInt64ForKey:KEY_PAYMENT_GOLD];
        mNumItemGlove = [decoder decodeIntForKey:KEY_ITEM_NUM_GLOVE];
        mNumItemXRay = [decoder decodeIntForKey:KEY_ITEM_NUM_XRAY];
        mNumItemTime = [decoder decodeIntForKey:KEY_ITEM_NUM_TIME];
        mNumItemMagnet = [decoder decodeIntForKey:KEY_ITEM_NUM_MAGNET];
    }
    
    return self;
}

-(void) saveUserInfo
{
    NSData *archivedObject = [NSKeyedArchiver archivedDataWithRootObject:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:archivedObject forKey:KEY_USER_INFO];
    [defaults synchronize];
}

-(void) setNumItem:(ITEM_TYPE)type num:(int)num
{
    switch (type) {
        case eIT_MAGNET:
            mNumItemMagnet = num;
            break;
            
        case eIT_SHIELD:
            mNumItemGlove = num;
            break;
            
        case eIT_TIMER:
            mNumItemTime = num;
            break;
            
        case eIT_XRAY:
            mNumItemXRay = num;
            break;
            
        default:
            break;
    }
}

-(int) getNumItem:(ITEM_TYPE)type
{
    switch (type) {
        case eIT_MAGNET:
            return mNumItemMagnet;
            
        case eIT_SHIELD:
            return mNumItemGlove;
            
        case eIT_TIMER:
            return mNumItemTime;
            
        case eIT_XRAY:
            return mNumItemXRay;
            
        default:
            return 0;
    }
}

@end
